package com.bhs.sansonglogistics.ui.order;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.Manifest;
import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseActivity;
import com.bhs.sansonglogistics.bean.BondedDevicesBean;
import com.bhs.sansonglogistics.utils.SharedPreferencesUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 蓝牙配置页
 */
public class BluetoothConfigurationActivity extends BaseActivity {

    private TextView mTvTitle;
    private RecyclerView mRvList;
    public BluetoothAdapter myBluetoothAdapter;
    private BaseQuickAdapter<BondedDevicesBean, BaseViewHolder> mAdapter;

    @Override
    public int initLayout() {
        return R.layout.activity_bluetooth_configuration;
    }

    @Override
    public void initView() {

        mTvTitle = findViewById(R.id.tv_title);
        mRvList = findViewById(R.id.rv_list);
        mTvTitle.setText("打印机配置");
    }

    @Override
    public void initData() {
        String bluetoothAddress = SharedPreferencesUtils.getString(mActivity, "bluetoothAddress");
        if ((myBluetoothAdapter = BluetoothAdapter.getDefaultAdapter()) == null) {
            Toast.makeText(this, "没有找到蓝牙适配器", Toast.LENGTH_LONG).show();
        }
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        if (!myBluetoothAdapter.isEnabled()) {
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
                return;
            }
            startActivityForResult(enableBtIntent, 2);
        }
        myBluetoothAdapter.getBondedDevices();
        Set<BluetoothDevice> pairedDevices = myBluetoothAdapter.getBondedDevices();
        List<BondedDevicesBean> list = new ArrayList<>();
        for (BluetoothDevice device : pairedDevices) {
            if (!TextUtils.isEmpty(bluetoothAddress) && bluetoothAddress.equals(device.getAddress())) {
                list.add(new BondedDevicesBean(device.getAddress(), device.getName(), true));
            } else {
                list.add(new BondedDevicesBean(device.getAddress(), device.getName()));
            }

        }
        mAdapter = new BaseQuickAdapter<BondedDevicesBean, BaseViewHolder>(R.layout.bluetooth_device, list) {
            @Override
            protected void convert(@NonNull BaseViewHolder helper, BondedDevicesBean item) {
                helper.setText(R.id.tv_address, item.getBluetoothAddress());
                helper.setImageResource(R.id.iv_status, item.isCheck() ? R.mipmap.ic_checked : R.mipmap.ic_unchecked);
            }
        };
        mRvList.setAdapter(mAdapter);
        mRvList.setLayoutManager(new LinearLayoutManager(mActivity));
        mAdapter.setEmptyView(LayoutInflater.from(mActivity).inflate(R.layout.layout_empty, null));
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                for (BondedDevicesBean bondedDevicesBean : list) {
                    bondedDevicesBean.setCheck(false);
                }
                list.get(position).setCheck(true);
                SharedPreferencesUtils.saveString(mActivity, "bluetoothAddress", list.get(position).getBluetoothAddress());
                mAdapter.notifyDataSetChanged();
            }
        });
    }
}